package com.yc.music.receiver;

import ohos.aafwk.content.Intent;
import ohos.agp.utils.TextTool;
import ohos.event.commonevent.CommonEventData;
import ohos.event.commonevent.CommonEventSubscribeInfo;
import ohos.event.commonevent.CommonEventSubscriber;

import com.yc.music.config.MusicPlayAction;
import com.yc.music.service.PlayService;
import com.yc.music.tool.BaseAppHelper;
import com.yc.music.utils.MusicLogUtils;

/**
 * NotificationStatusBarReceiver
 *
 * @since 2021-05-10
 */
public class NotificationStatusBarReceiver extends CommonEventSubscriber {
    /**
     * 操作状态栏
     */
    public static final String ACTION_STATUS_BAR = "YC_ACTION_STATUS_BAR";
    /**
     * 额外
     */
    public static final String EXTRA = "extra";

    /**
     * 构造函数
     *
     * @param subscribeInfo subscribeInfo
     */
    public NotificationStatusBarReceiver(CommonEventSubscribeInfo subscribeInfo) {
        super(subscribeInfo);
    }

    @Override
    public void onReceiveEvent(CommonEventData commonEventData) {
        Intent intent = commonEventData.getIntent();
        if (intent == null || TextTool.isNullOrEmpty(intent.getAction())) {
            return;
        }
        PlayService playService = new PlayService();
        String extra = intent.getStringParam(EXTRA);
        if (TextTool.isEqual(extra, MusicPlayAction.TYPE_NEXT)) {
            playService.startCommand(MusicPlayAction.TYPE_NEXT);
            MusicLogUtils.e("NotifiyStatusBarReceiver" + "下一首");
        } else if (TextTool.isEqual(extra, MusicPlayAction.TYPE_START_PAUSE)) {
            if (BaseAppHelper.get().getPlayService() != null) {
                boolean playing = BaseAppHelper.get().getPlayService().isPlaying();
                if (playing) {
                    MusicLogUtils.e("NotifiyStatusBarReceiver" + "暂停");
                } else {
                    MusicLogUtils.e("NotifiyStatusBarReceiver" + "播放");
                }
                playService.startCommand(MusicPlayAction.TYPE_START_PAUSE);
            }
        } else if (TextTool.isEqual(extra, MusicPlayAction.TYPE_PRE)) {
            playService.startCommand(MusicPlayAction.TYPE_PRE);
            MusicLogUtils.e("NotifiyStatusBarReceiver" + "上一首");
        }
    }
}
